package com.llws.petgoodsshop.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.llws.petgoodsshop.bean.Goods;
import com.llws.petgoodsshop.bean.GoodsVo;
import com.llws.petgoodsshop.bean.RtnInfo;
import com.llws.petgoodsshop.bean.Type;
import com.llws.petgoodsshop.mapper.GoodsMapper;
import com.mysql.cj.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.awt.print.Book;
import java.io.File;
import java.util.List;
import java.util.UUID;

@RestController
@RequestMapping("/goods")
public class GoodsController {
    @Autowired
    private GoodsMapper goodsMapper;

    @Value("${uploadPathImg}")
    private String uploadPathImg;   //图标图片存储路径

    /**
     * 头像上传
     * @param fileUpload 图片资源
     * @return 图映射的虚拟访问路径
     */
    @PostMapping("/uploadImg")
    public RtnInfo profilePhotoUpload(@RequestParam("file") MultipartFile fileUpload){
        //获取文件名
        String fileName = fileUpload.getOriginalFilename();
        //获取文件后缀名。也可以在这里添加判断语句，规定特定格式的图片才能上传，否则拒绝保存。
        String suffixName = fileName.substring(fileName.lastIndexOf("."));
        //为了避免发生图片替换，这里使用了文件名重新生成
        fileName = UUID.randomUUID()+suffixName;
        RtnInfo rtnInfo = new RtnInfo();
        try {
            //将图片保存到文件夹里
            File file = new File(uploadPathImg);
            if (!file.exists()){
                file.mkdirs();
            }
            fileUpload.transferTo(new File(uploadPathImg+fileName));
            rtnInfo.setCode(1);
            rtnInfo.setMsg("图片上传成功!");
            rtnInfo.setResult(fileName);
            return rtnInfo;
        } catch (Exception e) {
            e.printStackTrace();
            rtnInfo.setCode(-1);
            rtnInfo.setMsg("图片上传失败!");
            return rtnInfo;
        }
    }

    /**
     * 添加商品
     * @param goodsVo
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "add",method = RequestMethod.POST)
    public RtnInfo add(GoodsVo goodsVo) throws Exception{
        RtnInfo rtnInfo = new RtnInfo();
        if(StringUtils.isNullOrEmpty(goodsVo.getGoodsName())){
            rtnInfo.setCode(-1);
            rtnInfo.setMsg("商品名称不能为空！");
        }else if (goodsVo.getTypeId()==null){
            rtnInfo.setCode(-1);
            rtnInfo.setMsg("商品类型ID不能为空！");
        }else if(StringUtils.isNullOrEmpty(goodsVo.getProductionData())){
            rtnInfo.setCode(-1);
            rtnInfo.setMsg("商品生产日期不能为空！");
        } else {
            int count = goodsMapper.addGoods(goodsVo);
            if (count>0){
                rtnInfo.setCode(1);
                rtnInfo.setMsg("商品添加成功！");
            }
        }
        return rtnInfo;
    }

    /**
     *获取图书列表信息 支持分页显示
     * @param goodsName
     * @param currentPage
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "findAll/{goodsName}/{currentPage}",method = RequestMethod.GET)
    public RtnInfo findAll(@PathVariable(value = "goodsName",required = true)String goodsName,
                           @PathVariable(value = "currentPage",required = false)Integer currentPage) throws Exception{
        System.out.println("--进入到了商品列表查询："+goodsName);

        // 创建一个rtnInfo,封装响应到前端的信息
        RtnInfo rtnInfo = new RtnInfo();
        // 对关键字进行非空校验
        // 注意：如果要查询所有，就送一个 单引号占位置
        if(StringUtils.isNullOrEmpty(goodsName)|| goodsName.equals("''")){
            goodsName = "";
        }
        // 非空校验
        if(currentPage==null){
            currentPage = 1;// 如果没有获取到前端传递的当前页，就默认第一页
        }
        // 1. 访问数据库之前，需要设置分页条件,两个参数：pageNum-当前是第几页，pageSize=每页显示的条目数
        PageHelper.startPage(currentPage,10);
        // 2. 访问数据库
        List<Goods> list = goodsMapper.findGoodsAll(goodsName);
        for(Goods goods:list){
            String goodsPic = goods.getGoodsPic();
            goods.setGoodsPic("http://localhost:8080/pet/uploadImg/"+goodsPic);
        }
        // 3. 访问数据库之后，封装数据
        PageInfo<Goods> lists = new PageInfo<>(list,5);
        System.out.println("-----goods-list:"+lists);
        if(list!=null){
            // 封装信息到前端 http://localhost:8080/pet/uploadImg/

            rtnInfo.setCode(1);
            rtnInfo.setMsg("商品列表信息获取成功！");
            rtnInfo.setResult(lists);
        }else{
            // 封装信息到前端
            rtnInfo.setCode(0);
            rtnInfo.setMsg("商品列表信息获取失败！");
        }

        return rtnInfo;
    }

    /**
     * 修改图书信息
     * @param goodsVo
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "update",method = RequestMethod.POST)
    public RtnInfo update(GoodsVo goodsVo) throws Exception{
        // 创建一个rtnInfo，用来封装需要响应到前端的信息
        RtnInfo rtnInfo = new RtnInfo();
        // 非空检验
        if(StringUtils.isNullOrEmpty(goodsVo.getGoodsId()+"")){
            rtnInfo.setCode(-1);
            rtnInfo.setMsg("商品编号不能为空！");
        }else if(StringUtils.isNullOrEmpty(goodsVo.getGoodsPic())){
            rtnInfo.setCode(-1);
            rtnInfo.setMsg("商品图片不能为空！");
        }else if(StringUtils.isNullOrEmpty(goodsVo.getGoodsName())){
            rtnInfo.setCode(-1);
            rtnInfo.setMsg("商品不能为空！");
        }else if(StringUtils.isNullOrEmpty(goodsVo.getDepict())){
            rtnInfo.setCode(-1);
            rtnInfo.setMsg("商品描述不能为空！");
        }else{
            // 访问数据库
            if(goodsMapper.updateGoods(goodsVo)>0){
                rtnInfo.setCode(1);
                rtnInfo.setMsg("商品修改成功！");
            }else{
                rtnInfo.setCode(0);
                rtnInfo.setMsg("商品修改失败！");
            }
        }
        return rtnInfo;
    }
    /**
     * 删除图书信息
     * 请求方式：GET
     * 请求URL：/pet/goods/delete/{goodsId}
     * 响应结果：JSON
     * @param goodsId
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "delete/{goodsId}")
    public RtnInfo delete(@PathVariable(value = "goodsId",required = true)Integer goodsId) throws Exception{
        RtnInfo rtnInfo = new RtnInfo();
        // 访问数据库
        if(goodsMapper.deleteGoods(goodsId)>0){
            rtnInfo.setCode(1);
            rtnInfo.setMsg("图书信息删除成功！");
        }else{
            rtnInfo.setCode(0);
            rtnInfo.setMsg("图书信息删除失败！");
        }
        return rtnInfo;
    }

    /**
     * 图书上架和下架管理
     * 请求方式：GET
     * 请求URL：/pet/goods/updateStatus/${goodsId}/${status}
     * @param goodsId
     * @param status
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "updateStatus/{goodsId}/{status}",method = RequestMethod.GET)
    public RtnInfo updateStatus(@PathVariable(value = "goodsId",required = true)Integer goodsId,
                                @PathVariable(value = "status",required = true)Integer status) throws Exception{
        RtnInfo rtnInfo = new RtnInfo();
        // 访问数据库
        Goods goods = new Goods();
        goods.setGoodsId(goodsId);
        goods.setStatus(status);
        if(goodsMapper.updateStatus(goods)>0){
            rtnInfo.setCode(1);
            if(status==1){
                rtnInfo.setMsg("图书上架成功！");
            }else{
                rtnInfo.setMsg("图书下架成功！");
            }
        }else{
            rtnInfo.setCode(0);
            if(status==1){
                rtnInfo.setMsg("图书上架失败！");
            }else{
                rtnInfo.setMsg("图书下架失败！");
            }
        }
        return rtnInfo;
    }
    /**
     *获取上架图书列表信息 支持分页显示
     * @param goodsName
     * @param currentPage
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "findUpAll/{goodsName}/{currentPage}",method = RequestMethod.GET)
    public RtnInfo goodsUp(@PathVariable(value = "goodsName",required = true)String goodsName,
                           @PathVariable(value = "currentPage",required = false)Integer currentPage) throws Exception{
        System.out.println("--进入到了商品列表查询："+goodsName);

        // 创建一个rtnInfo,封装响应到前端的信息
        RtnInfo rtnInfo = new RtnInfo();
        // 对关键字进行非空校验
        // 注意：如果要查询所有，就送一个 单引号占位置
        if(StringUtils.isNullOrEmpty(goodsName)|| goodsName.equals("''")){
            goodsName = "";
        }
        // 非空校验
        if(currentPage==null){
            currentPage = 1;// 如果没有获取到前端传递的当前页，就默认第一页
        }
        // 1. 访问数据库之前，需要设置分页条件,两个参数：pageNum-当前是第几页，pageSize=每页显示的条目数
        PageHelper.startPage(currentPage,10);
        // 2. 访问数据库
        List<Goods> list = goodsMapper.findUpAll(goodsName);
        for(Goods goods:list){
            String goodsPic = goods.getGoodsPic();
            goods.setGoodsPic("http://localhost:8080/pet/uploadImg/"+goodsPic);
        }
        // 3. 访问数据库之后，封装数据
        PageInfo<Goods> lists = new PageInfo<>(list,5);
        System.out.println("-----goods-list:"+lists);
        if(list!=null){
            // 封装信息到前端 http://localhost:8080/pet/uploadImg/
            rtnInfo.setCode(1);
            rtnInfo.setMsg("上架商品列表信息获取成功！");
            rtnInfo.setResult(lists);
        }else{
            // 封装信息到前端
            rtnInfo.setCode(0);
            rtnInfo.setMsg("上架商品列表信息获取失败！");
        }
        return rtnInfo;
    }

    /**
     *获取下架图书列表信息 支持分页显示
     * @param goodsName
     * @param currentPage
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "findDownAll/{goodsName}/{currentPage}",method = RequestMethod.GET)
    public RtnInfo goodsDown(@PathVariable(value = "goodsName",required = true)String goodsName,
                           @PathVariable(value = "currentPage",required = false)Integer currentPage) throws Exception{
        System.out.println("--进入到了商品列表查询："+goodsName);

        // 创建一个rtnInfo,封装响应到前端的信息
        RtnInfo rtnInfo = new RtnInfo();
        // 对关键字进行非空校验
        // 注意：如果要查询所有，就送一个 单引号占位置
        if(StringUtils.isNullOrEmpty(goodsName)|| goodsName.equals("''")){
            goodsName = "";
        }
        // 非空校验
        if(currentPage==null){
            currentPage = 1;// 如果没有获取到前端传递的当前页，就默认第一页
        }
        // 1. 访问数据库之前，需要设置分页条件,两个参数：pageNum-当前是第几页，pageSize=每页显示的条目数
        PageHelper.startPage(currentPage,10);
        // 2. 访问数据库
        List<Goods> list = goodsMapper.findDownAll(goodsName);
        for(Goods goods:list){
            String goodsPic = goods.getGoodsPic();
            goods.setGoodsPic("http://localhost:8080/pet/uploadImg/"+goodsPic);
        }
        // 3. 访问数据库之后，封装数据
        PageInfo<Goods> lists = new PageInfo<>(list,5);
        System.out.println("-----goods-list:"+lists);
        if(list!=null){
            // 封装信息到前端 http://localhost:8080/pet/uploadImg/
            rtnInfo.setCode(1);
            rtnInfo.setMsg("上架商品列表信息获取成功！");
            rtnInfo.setResult(lists);
        }else{
            // 封装信息到前端
            rtnInfo.setCode(0);
            rtnInfo.setMsg("上架商品列表信息获取失败！");
        }
        return rtnInfo;
    }
}
